package com.yzyz.web.controller.visitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.yzyz.bean.route.RouteBean;
import com.yzyz.bean.scenic.ScenicBean;
import com.yzyz.data.CacheFactory;

/**
 * 
 * @author David Sun
 * @Date   2010-4-10
 *
 */
public class RouteController extends MultiActionController {
	public ModelAndView getRoute(HttpServletRequest request, HttpServletResponse response){
		if(request.getParameter("routeId") == null) {
			return null;
		}
		int routeId = Integer.valueOf(request.getParameter("routeId"));
		int groupId = Integer.valueOf(request.getParameter("routeGroupId"));
		RouteBean route = CacheFactory.getRouteGroupStore().getRouteByRouteIdAndGroupId(routeId, groupId);
		if(route == null){
			ModelAndView mv = new ModelAndView("/Error/error.jsp");
			mv.addObject("errorMsg", "对不起！没有找到您想查看的行程信息");
			return mv;
		}
		List<ScenicBean> scenics = route.getScenics();
		
		List<ScenicBean> cachedScenics = new ArrayList<ScenicBean>();
		Map<Integer, RouteBean> routes = new HashMap<Integer, RouteBean>();
		if(scenics != null){
			for(ScenicBean scenic: scenics){
				cachedScenics.add(CacheFactory.getRegionStore().getScenicById(scenic.getId()));
			}
		}
		for(ScenicBean scenic: cachedScenics){
			if(scenic.getRoutes() != null){
				for(RouteBean r: scenic.getRoutes()){
					routes.put(r.getId(), r);
				}
			}
		}
		routes.remove(route.getId());
		ModelAndView mv = new ModelAndView("/Route/Info/routeInfo.jsp");
		mv.addObject("route", route);
		mv.addObject("scenics", cachedScenics);
		mv.addObject("relatedRoutes", routes);
		return mv;
	}
	
	public void getAttractions(HttpServletRequest request, HttpServletResponse response) {
		
	}
}
